﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace SmsWeb
{
    public partial class Default : System.Web.UI.Page
    {
        SmsEntities entities = new SmsEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtContent.Text == "")
            {
                return;
            }

            try
            {
                银行短信记录 model = new 银行短信记录();
                //model.短信日期 = DateTime.Now;
                model.短信编号 = "";
                model.短信发布时间 = DateTime.Now;
                model.短信内容 = this.txtContent.Text;
                model.手机号码 = this.radPhoneNumber.SelectedValue;
                Process(model);
                entities.银行短信记录.AddObject(model);
                entities.SaveChanges();
                this.txtContent.Text = "";
                labMessage.Text = "保存成功！";
                labMessage.Visible = true;
            }
            catch
            {
                labMessage.Text = "保存失败，请重试或联系管理员！";
                labMessage.ForeColor = Color.Red;
                labMessage.Visible = true;
            }
        }

        private void Process(银行短信记录 model)
        {
            if (string.IsNullOrEmpty(model.短信内容))
            {
                return;
            }

            int start, end;
            start = model.短信内容.IndexOf("余额");
            if (start < 0) { return; }
            end = model.短信内容.IndexOf("元", start);
            if (end < 0) { return; }
            model.余额信息 = model.短信内容.Substring(start, end - start + 1);
            if (start - 1 >= 0 && model.短信内容[start - 1] == ',' || model.短信内容[start - 1] == '，')
            {
                start--;
            }
            model.短信内容 = model.短信内容.Remove(start, end - start + 1);
        }
    }
}